/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.service.project.imported;

import cn.ac.nercis.pes.common.constant.knowledge.AssetClassification;
import cn.ac.nercis.pes.common.constant.knowledge.TargetDeepness;
import cn.ac.nercis.pes.model.dto.evaluation.asset.*;
import cn.ac.nercis.pes.model.standard.asset.AssetSDTO;
import cn.ac.nercis.pes.repository.mapper.evaluation.AssetMapper;
import cn.ac.nercis.pes.repository.mapper.standard.SystemImportMapper;
import cn.ac.nercis.pes.repository.model.evaluation.AssetDO;
import cn.ac.nercis.pes.repository.model.project.SystemTargetDO;
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 导入服务基础功能
 *
 * @author zpy
 * @version 1.0.0
 */
public abstract class CommonImportService {

    protected String getTargetName(String targetId, List<SystemTargetDO> systemTargets) {
        var targetDO = systemTargets.stream()
                .filter(target -> StringUtils.equals(target.getKmTargetId(), targetId))
                .findFirst().orElse(null);
        if (Objects.isNull(targetDO)) {
            return "";
        }
        if (targetDO.getKmDeepness() == TargetDeepness.LAYER.getDeepness()) {
            return targetDO.getKmName();
        } else {
            return systemTargets.stream()
                    .filter(t -> StringUtils.equals(t.getId(), targetDO.getKmParentId()))
                    .map(lt -> lt.getKmName().concat("/").concat(targetDO.getKmName()))
                    .findFirst().orElse(targetDO.getKmName());
        }
    }

    protected AssetDO findAsset(String importAssetId, List<AssetSDTO> importAssets, List<AssetDO> sourceAssets) {
        var assetSDTO = importAssets.stream()
                .filter(a -> StringUtils.equals(a.getId(), importAssetId))
                .findFirst().orElse(null);
        if (Objects.isNull(assetSDTO)) {
            return null;
        }
        AssetClassification classification = AssetClassification.fromId(assetSDTO.getClassId());
        List<AssetDO> assets = sourceAssets.stream()
                .filter(a -> StringUtils.equals(a.getClassId(), classification.getId()))
                .collect(Collectors.toList());
        return switch (classification) {
            case PHY_ENV -> findAssetByPhyEnv(SystemImportMapper.INSTANCE.toPhyEnvDTO(assetSDTO), assets);
            case PHY_SECURITY -> findAssetByPhySecurity(SystemImportMapper.INSTANCE.toPhySecurity(assetSDTO), assets);
            case PWD_PRODUCT -> findAssetByPwdProduct(SystemImportMapper.INSTANCE.toPwdProduct(assetSDTO), assets);
            case SERVER_STORAGE ->
                    findAssetByServerStorage(SystemImportMapper.INSTANCE.toServerStorage(assetSDTO), assets);
            case NETWORK_SECURITY ->
                    findAssetByNetworkSecurity(SystemImportMapper.INSTANCE.toNetworkSecurity(assetSDTO), assets);
            case DATABASE -> findAssetByDatabase(SystemImportMapper.INSTANCE.toDatabase(assetSDTO), assets);
            case KEY_BUSINESS -> findAssetByKeyBusiness(SystemImportMapper.INSTANCE.toKeyBusiness(assetSDTO), assets);
            case SUB_BUSINESS -> findAssetBySubBusiness(SystemImportMapper.INSTANCE.toSubBusiness(assetSDTO), assets);
            case KEY_DATA -> findAssetByKeyData(SystemImportMapper.INSTANCE.toKeyData(assetSDTO), assets);
            default -> findAssetByName(assetSDTO.getName(), assets);
        };
    }

    protected AssetDO findAssetByPhyEnv(@NonNull PhyEnvDTO importDTO, List<AssetDO> sourceAssets) {
        return sourceAssets.stream()
                .filter(assetDO -> {
                    PhyEnvDTO sourceDTO = AssetMapper.INSTANCE.toPhyEnvDTO(assetDO);
                    return Objects.nonNull(sourceDTO)
                            && StringUtils.equals(sourceDTO.getName(), importDTO.getName())
                            && StringUtils.equals(sourceDTO.getLocation(), importDTO.getLocation());
                })
                .findFirst()
                .orElse(null);
    }

    protected AssetDO findAssetByPhySecurity(@NonNull PhySecurityDTO importDTO, List<AssetDO> sourceAssets) {
        return sourceAssets.stream()
                .filter(assetDO -> {
                    PhySecurityDTO sourceDTO = AssetMapper.INSTANCE.toPhySecurity(assetDO);
                    return Objects.nonNull(sourceDTO)
                            && StringUtils.equals(sourceDTO.getName(), importDTO.getName())
                            && StringUtils.equals(sourceDTO.getLocation(), importDTO.getLocation());
                })
                .findFirst()
                .orElse(null);
    }

    protected AssetDO findAssetByPwdProduct(@NonNull PwdProductDTO importDTO, List<AssetDO> sourceAssets) {
        return sourceAssets.stream()
                .filter(assetDO -> {
                    PwdProductDTO sourceDTO = AssetMapper.INSTANCE.toPwdProduct(assetDO);
                    return Objects.nonNull(sourceDTO)
                            && StringUtils.equals(sourceDTO.getName(), importDTO.getName())
                            && StringUtils.equals(sourceDTO.getModel(), importDTO.getModel());
                })
                .findFirst()
                .orElse(null);
    }

    protected AssetDO findAssetByServerStorage(@NonNull ServerStorageDTO importDTO, List<AssetDO> sourceAssets) {
        return sourceAssets.stream()
                .filter(assetDO -> {
                    ServerStorageDTO sourceDTO = AssetMapper.INSTANCE.toServerStorage(assetDO);
                    return Objects.nonNull(sourceDTO)
                            && StringUtils.equals(sourceDTO.getName(), importDTO.getName())
                            && StringUtils.equals(sourceDTO.getVendor(), importDTO.getVendor())
                            && StringUtils.equals(sourceDTO.getModel(), importDTO.getModel());
                })
                .findFirst()
                .orElse(null);
    }

    protected AssetDO findAssetByNetworkSecurity(@NonNull NetworkSecurityDTO importDTO, List<AssetDO> sourceAssets) {
        return sourceAssets.stream()
                .filter(assetDO -> {
                    NetworkSecurityDTO sourceDTO = AssetMapper.INSTANCE.toNetworkSecurity(assetDO);
                    return Objects.nonNull(sourceDTO)
                            && StringUtils.equals(sourceDTO.getName(), importDTO.getName())
                            && StringUtils.equals(sourceDTO.getVendor(), importDTO.getVendor())
                            && StringUtils.equals(sourceDTO.getModel(), importDTO.getModel());
                })
                .findFirst()
                .orElse(null);
    }

    protected AssetDO findAssetByDatabase(@NonNull DatabaseDTO importDTO, List<AssetDO> sourceAssets) {
        return sourceAssets.stream()
                .filter(assetDO -> {
                    DatabaseDTO sourceDTO = AssetMapper.INSTANCE.toDatabase(assetDO);
                    return Objects.nonNull(sourceDTO)
                            && StringUtils.equals(sourceDTO.getName(), importDTO.getName())
                            && StringUtils.equals(sourceDTO.getLocation(), importDTO.getLocation());
                })
                .findFirst()
                .orElse(null);
    }

    protected AssetDO findAssetByKeyBusiness(@NonNull KeyBusinessDTO importDTO, List<AssetDO> sourceAssets) {
        return sourceAssets.stream()
                .filter(assetDO -> {
                    KeyBusinessDTO sourceDTO = AssetMapper.INSTANCE.toKeyBusiness(assetDO);
                    return Objects.nonNull(sourceDTO)
                            && StringUtils.equals(sourceDTO.getName(), importDTO.getName())
                            && StringUtils.equals(sourceDTO.getLocation(), importDTO.getLocation());
                })
                .findFirst()
                .orElse(null);
    }

    protected AssetDO findAssetBySubBusiness(@NonNull SubBusinessDTO importDTO, List<AssetDO> sourceAssets) {
        return sourceAssets.stream()
                .filter(assetDO -> {
                    SubBusinessDTO sourceDTO = AssetMapper.INSTANCE.toSubBusiness(assetDO);
                    return Objects.nonNull(sourceDTO)
                            && StringUtils.equals(sourceDTO.getName(), importDTO.getName())
                            && StringUtils.equals(sourceDTO.getOwnedApp(), importDTO.getOwnedApp());
                })
                .findFirst()
                .orElse(null);
    }

    protected AssetDO findAssetByKeyData(@NonNull KeyDataDTO importDTO, List<AssetDO> sourceAssets) {
        return sourceAssets.stream()
                .filter(assetDO -> {
                    KeyDataDTO sourceDTO = AssetMapper.INSTANCE.toKeyData(assetDO);
                    return Objects.nonNull(sourceDTO)
                            && StringUtils.equals(sourceDTO.getData(), importDTO.getData())
                            && StringUtils.equals(sourceDTO.getOwnedApp(), importDTO.getOwnedApp())
                            && StringUtils.equals(sourceDTO.getLocation(), importDTO.getLocation());
                })
                .findFirst()
                .orElse(null);
    }

    protected AssetDO findAssetByName(@NonNull String assetName, List<AssetDO> sourceAssets) {
        return sourceAssets.stream()
                .filter(a -> StringUtils.equals(a.getName(), assetName))
                .findFirst().orElse(null);
    }

}